<?php

namespace App\Http\Controllers\Home;

use App\Model\talk;
use App\Model\writer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class userTalkController extends Controller
{
    //Request是http请求
    public function show(Request $request, $id=0)
    {
        if (!empty($id)){
            $id=session('writerid');
        }
        //判断是否用post传输
        if ($request->isMethod('post')){
            //根据$id来查询作家表中对应的id
            $new = writer::where('id',$id)->get();
            //遍历取出头像和笔名
            foreach($new as $k){
                $icon=$k->icon;
                $nickname=$k->nickname;
            }
            //新增一个表
            $add = new talk();
            $add->wid = $id;
            $add->nickname = $nickname;
            $add->icon = $icon;
            $add->type = $request->type;
            $rules = array('text'=>'required');
            $mess = array('text.required'=>'内容不可为空');
            $this->validate($request,$rules,$mess);
            $add->text = $request->text;
            $add->can = 1;
            $add->addtime=time();
            $add->save();
            return redirect('huweize/home/userTalk/lll/'.$id);
        }
        $talks = talk::where('wid',$id)->paginate(3);
        return view('huweize.home.userTalk',compact('talks','id'));
    }

    public function del(Request $request, $id)
    {
        $new = talk::where('id',$id);
        foreach($new as $k){
            $wid=$k->wid;
        }
        $new->delete();
        return back();
    }

    public function delall($id)
    {
        $del = talk::where('wid',$id)->delete();
        return redirect('huweize/home/userTalk/lll/'.session('writerid'));
    }

}
